<?php

namespace app\api\controller;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 招聘会公司
 */
class InviteEnterprise  extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\InviteEnterprise();
        $this->modelinvite = new \app\api\model\Invite();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where = [];

            if (!empty($input['invite_address_id'])) {
                $where['m.invite_address_id'] = $input['invite_address_id'];
            }
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->alias('m')
                ->join('enterprise_user u','m.enterprise_user_id=u.id')
                ->where($where)
                ->order('m.createtime asc')
                ->field('u.*')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);
            foreach ($data->items() as &$v){
                $v['invite'] =$this->modelinvite
                    ->where('user_id',$v['user_id'])->select();
            }
            $this->success('ok', $data);
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

}